package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.model.auto.SpecialCategory;
import com.service.ISpecialCategoryService;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2022-09-14
 */
@RestController
@RequestMapping("/specialCategory")
public class SpecialCategoryController{
    @Autowired
    private ISpecialCategoryService specialCategoryService;

    @GetMapping("/getSpecialCategory")
    public R getSpecialCategory(String stuId) {
        SpecialCategory specialCategory = specialCategoryService.getOne(new LambdaQueryWrapper<SpecialCategory>()
                .eq(SpecialCategory::getStuId, stuId));
        return R.ok(specialCategory);
    }

    @PostMapping("SaveOrUpdateSpecialCategory")
    public R SaveOrUpdateSpecialCategory(@RequestBody SpecialCategory specialCategory) {
        //先删除之前保存的
        SpecialCategory removeEntity = new SpecialCategory();
        removeEntity.setStuId(specialCategory.getStuId());
        specialCategoryService.removeById(removeEntity);

        boolean result = specialCategoryService.saveOrUpdate(specialCategory);
        if (!result) {
            return R.error();
        }
        return R.ok();
    }
}
